<style>
.tree {
  /*margin-top: 42px;*/
  border: 1px solid #d7dde4;
  /* max-height: 600px; */
}

</style>
<template>
<Row class="layout-content-body">
  <Col span="4" class="tree">
      <Tree ref="typetree" :data="typetree" ></Tree>
  </Col>
  <Col span="20">
  <dtable :columns="columns" :dataAll="dataAll" :totals="total" 
  add check op 
  @refresh="getall()" @add="add" @del="del" @edit="edit"  :loading="loading"></dtable>
  </Col>
  <Modal title="产品类型信息" v-model="editData" :mask-closable="false" class="editm" @on-ok="asyncOK" @on-cancel="asyncCancel">
    <Form ref="editForm" :model="formItem" :label-width="80" :rules="ruleValidate">
      <Form-item label="名称" prop="Name">
        <Input v-model="formItem.Name" placeholder="请输入" @on-enter="asyncOK()"></Input>
      </Form-item>
      <Form-item label="父类型" prop="Pname">
        <Input v-model="formItem.Pname"  placeholder="请输入" @on-focus="SelectType()"></Input>
      </Form-item>
      <Form-item label="说明" prop="Remark">
        <Input v-model="formItem.Remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="请输入...">
        </Input>
      </Form-item>
    </Form>
  </Modal>
  <Modal title="上级类型"  v-model="typeshow" @on-ok="typeok" >
    <div span="4" class="tree">
        <Tree ref="Ptree" :data="typetree" ></Tree>
    </div>
  </Modal>
</Row>
</template>

<script>
// import api from '../../api'
// import { mapMutations, mapGetters, mapState } from 'vuex'
import dtable from '@/components/table/dtable.vue' //采用biztable
export default {
  data() {
    return {
      loading: false, //点击确定按钮时，确定按钮是否显示 loading 状态，开启则需手动设置visible来关闭对话框
      spinShow: false, //数据刷新遮罩层
      columns: [
        //多选框
        {
          title: '名称',
          key: 'Name',
          sortable: true
        },
        {
          title: '上级类型',
          key: 'Pname',
          sortable: true
        },
        {
          title: '说明',
          key: 'Remark',
          sortable: true,
        },
        {
          title: '创建人',
          key: 'Creater',
          sortable: true
        },
        {
          title: '创建时间',
          key: 'Ctime',
          sortable: true
        },
      ], //表格样式
      dataAll: [], //表格数据来源
      dataShow: true, //是否显示表格数据查看表单
      editData: false, //显示编辑表单
      formItem: {
        Id: '0',
        Name: '',
        Pname:'顶级目录',
        Pid: '0',
        Remark: ''
      }, //数据编辑表单
      ruleValidate: {
        Name: [{
          required: true,
          message: '名称不能为空',
          trigger: 'blur'
        }],
      }, //数据编辑表单验证规则
      addid: true, //true为修改模式，false为添加模式
      typetree:[],
      typeshow:false,
      total: 0
    }
  },
  components: {
    dtable,
  },
  computed: {

  },
  watch: {},
  methods: {
    getall() {
      this.loading = true
      this.$api.getProdType().then(res => {
        if (!res.error) {
          console.log(res.Data)
          this.dataAll = res.Data.tabledata
          this.typetree=res.Data.treedata
          if (this.dataAll) {
            this.total = (this.dataAll).length
          }
        } else {
          this.$Message.error(res.message)
        }
      })
      setTimeout(() => {
        this.loading = false;
      }, 0);
    },
    edit(index) {
      this.addid = true
      this.formItem = index
      this.editData = true
    },
    add() {
      this.addid = false
      this.$refs['editForm'].resetFields()
      this.editData = true
    },
    asyncOK() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          if (this.addid) {
            this.$api.updateProdType(this.formItem).then(res => {
              if (!res.error) {
                this.getall()
                this.$Message.success(res.message)
              } else {
                this.$Message.error(res.message, 4)
              }
            })
          } else if (!this.addid) {
            this.$api.addProdType(this.formItem).then(res => {
              if (!res.error) {
                this.getall()
                this.$Message.success(res.message)
              } else {
                this.$Message.error(res.message, 4)
              }
            })
          }
          setTimeout(() => {
            this.editData = false;
          }, 0);
        } else {
          this.$Message.error('表单验证失败!');
        }
      })
      this.editData = false
      this.$refs['editForm'].resetFields()

    },
    asyncCancel() {
      this.getall()
      this.$refs['editForm'].resetFields()
    },
    del(row) {
      this.$api.deleteProdType(row).then(res => {
        if (!res.error) {
          this.getall()
          this.$Message.success(res.message)
        } else {
          this.$Message.error(res.message, 4)
        }
      })
    },
    delCheck(row) {

    },
    SelectType(){
      this.typeshow=true
    },
    typeok(){
      console.log(this.$refs.Ptree.getSelectedNodes()[0])
      this.formItem.Pid=this.$refs.Ptree.getSelectedNodes()[0].id
      this.formItem.Pname=this.$refs.Ptree.getSelectedNodes()[0].title
    }

  },
  mounted() {
    this.getall()
  }
}
</script>
